package com.hsmpay.mobile.service.user.impl;

import com.hsmpay.common.pojo.user.Person;
import com.hsmpay.common.pojo.user.User;
import com.hsmpay.common.service.impl.BaseServiceSupport;
import com.hsmpay.mobile.dao.user.UserDPIDao;
import com.hsmpay.mobile.pojo.user.UserDPI;
import com.hsmpay.mobile.service.user.UserDPIService;
import com.hsmpay.mobile.service.user.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.sql.SQLException;

/**
 * 用户实名认证 userDPIService
 *
 * @param <Entity>
 * @param <Query>
 * @author 颜铃璋
 *         建立时间：2013-2-25
 */
@Service("userDPIService")
public class UserDPIServiceImpl extends BaseServiceSupport<UserDPI, Long> implements UserDPIService<UserDPI, Long> {
    @Resource(name = "userDPIDao")
    private UserDPIDao<UserDPI, Long> userDPIDao;
    //	@Resource(name="userDPILogDao")
//	private UserDPILogDao<UserDPILog,Long> userDPILogDao;
    @Resource(name = "userService")
    private UserService<User, Long> userService;


    /**
     * 实名认证
     *
     * @param userDPI
     * @param user
     * @return
     * @throws Exception
     */
    public int userDPI(UserDPI userDPI, User user) throws Exception {
        //删除以前 提交的实名认证  将这个实名认证  挪到日志表里面去
        int flag = updateAllTopHistory(user.getId());

        //int flag = userService.bindingBankCard(user);//修改用户 这里用绑定银行接口
        userDPIDao.insertEntity(userDPI);

        //--- 更新Person表中的实名认证状态,使其与新增的DPI状态同步 ---   实名认证的时候 修改该问题
        Person person = new Person();
        person.setUserId(user.getId());
        person.setDPIStatus(userDPI.getStatus());
        person.setUserName(user.getUserName());
        flag = userService.updateDPIStatus(person, user);

        return flag;
    }

//	private int moveUserDPIToLog(UserDPI errorDPI)throws Exception{
//		int flag = 0;
//		if(null != errorDPI){
//			UserDPILog  userDPILog= new UserDPILog();
//			BeanUtils.copyProperties(errorDPI,userDPILog);
//			userDPILogDao.insertEntity(userDPILog);
////			userDPIDao.deleteEntity(errorDPI);
//			//修改为日志信息 
//			errorDPI.setIsHistory(1);
//			flag = userDPIDao.updateEntity(errorDPI);
//		}
//		return flag;
//	}

    /**
     * 查询最后一次实名认证 失败的信息
     *
     * @param terminalTypeId
     * @param roleId
     * @param userId
     * @return
     * @throws Exception
     */
    public UserDPI searchLatestUserDPIError(Long terminalTypeId, Long roleId, Long userId) throws Exception {
        UserDPI userDPI = new UserDPI();
        //userDPI.setTerminalTypeId(terminalTypeId);
        //userDPI.setRoleId(roleId);
        userDPI.setUserId(userId);
        userDPI.setStatus(0);
        return userDPIDao.searchLatestUserDPI(userDPI);
    }

    /**
     * 修改所有实名认证为历史版本
     *
     * @param userDPI
     * @return
     * @throws SQLException
     */
    public int updateAllTopHistory(Long userId) throws Exception {
        UserDPI userDPI = new UserDPI();
        userDPI.setIsHistory(1);
        userDPI.setUserId(userId);
        return userDPIDao.updateAllTopHistory(userDPI);
    }
    //---------------------set get start--------------------
    //---------------------set get end----------------------
}
